package xq.gwt.ui.widgets.command;

import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;
import xq.gwt.mvc.controller.CommandController;
import xq.gwt.mvc.model.CommandModel;
import xq.gwt.mvc.view.CommandView;
import xq.gwt.mvc.view.ObservableViewImp;
import xq.gwt.ui.widgets.feedback.DefaultExceptionPresenter;
import xq.gwt.ui.widgets.feedback.DefaultFeedbackPresenter;

import com.smartgwt.client.widgets.events.ClickEvent;


public class ToolStripButtonCommandPresenter extends ObservableViewImp implements CommandView {
	protected CommandController controller;
	protected ToolStripButton button;
	
	public ToolStripButtonCommandPresenter(){
		button = new ToolStripButton();
		init();
	}
	
	public ToolStripButtonCommandPresenter(ToolStripButton button){
		this.button = button;
		init();
	}

	private void init(){
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent	event) {
				notifyViewChangedListeners();
			}
		});
		controller = new CommandController(this);
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		if(enabled)
			button.enable();
		else {
			button.disable();			
		}
	}

	@Override
	public void showFeedback(String feedback) {
		DefaultFeedbackPresenter presenter = new DefaultFeedbackPresenter();
		presenter.showFeedback(feedback);
		
	}

	@Override
	public void reportException(Exception ex) {
		DefaultExceptionPresenter exPresenter = new DefaultExceptionPresenter();
		exPresenter.showException(ex);
		
	}

	@Override
	public void executeStarted() {
		button.disable();
		
	}

	@Override
	public void excuteFinished() {
		button.enable();
		
	}

	@Override
	public void setCaption(String caption) {
		button.setTitle(caption);
		
	}
	
	public ToolStripButton getButton() {
		return button;
	}
	
	public void setModel(CommandModel model){
		controller.setCommandModel(model);
	}
}
